package io.easyspring.demo.basis.rbac.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.github.pagehelper.Page;
import io.easyspring.framework.base.form.BaseSortForm;
import io.easyspring.framework.common.exception.CommonException;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;

import io.easyspring.demo.basis.rbac.model.User;
import io.easyspring.framework.base.pagehelper.PageInfo;
import io.easyspring.framework.base.view.CommonJsonView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.data.domain.Pageable;
import io.easyspring.demo.basis.rbac.service.UserService;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * 用户对象 前端控制器
 *
 * @author summer
 * @version 2.0.0-RELEASE
 * DateTime 2019-10-15 15:15:03
 */
@RestController
@RequestMapping("/rbac/user")
@Validated
@Slf4j
@Api(value = "/rbac/user", tags = "用户对象 API")
public class UserController {

    /**
     * 注入 用户对象 的 service
     */
    @Resource
    private UserService userService;


    /**
     * 用户对象 的添加方法
     *
     * @param user 用户对象
     * @return io.easyspring.demo.basis.rbac.model.User
     * Author summer
     * Version 2.0.0-RELEASE
     * DateTime 2019-10-15 15:15:03
     */
    @ApiOperation(value = "添加", notes = "用户对象 的添加方法")
    @JsonView(CommonJsonView.SimpleView.class)
    @PostMapping
    public User save(@NotNull(message = "用户对象 不能为空") @Valid
                                @RequestBody User user){
        // 执行添加操作并返回保存后的对象
        return userService.buildSave(user);
    }

    /**
     * 根据传入的 id 删除对应的 用户对象
     *
     * @param id 想要删除的数据的 id
     * @return boolean
     * Author summer
     * Version 2.0.0-RELEASE
     * DateTime 2019-10-15 15:15:03
     */
    @ApiOperation(value = "删除", notes = "根据传入的 id 删除对应的 用户对象")
    @DeleteMapping("/{id:\\d+}")
    public boolean delete(@PathVariable(value = "id") Long id) {
        // 执行删除并返回结果
        return userService.delete(id) > 0;
    }

    /**
     * 用户对象 的修改方法
     *
     * @param user 用户对象
     * @return io.easyspring.demo.basis.rbac.model.User
     * Author summer
     * Version 2.0.0-RELEASE
     * DateTime 2019-10-15 15:15:03
     */
    @ApiOperation(value = "修改", notes = "用户对象的修改方法")
    @JsonView(CommonJsonView.SimpleView.class)
    @PutMapping
    public User update(@NotNull(message = "用户对象不能为空") @Valid
                                    @RequestBody User user){
        // 执行添加操作并返回保存后的对象
        return userService.buildUpdate(user);
    }

    /**
     * 根据 id 查询单个 用户对象
     *
     * @param id 用户对象 id
     * @return io.easyspring.demo.basis.rbac.model.User
     * Author summer
     * Version 2.0.0-RELEASE
     * DateTime 2019-10-15 15:15:03
     */
    @ApiOperation(value = "查询单个", notes = "根据 id 查询单个用户对象")
    @JsonView(CommonJsonView.DetailView.class)
    @GetMapping("/{id:\\d+}")
    public User get(@PathVariable(value = "id") Long id) {
        return userService.get(id);
    }

    /**
     * 根据传入的条件, 查询符合条件的用户对象的集合
     *
     * @param user 查询条件
     * @return java.util.List<io.easyspring.demo.basis.rbac.model.User>
     * Author summer
     * Version 2.0.0-RELEASE
     * DateTime 2019-10-15 15:15:03
     */
    @ApiOperation(value = "列表查询", notes = "根据传入的条件, 查询符合条件的用户对象的集合")
    @JsonView(CommonJsonView.SimpleView.class)
    @GetMapping
    public List<User> list(User user) {
        // 执行查询并返回相关数据
        return userService.list(user);
    }

    /**
     * 快速排序的接口
     *
     * @param baseSortFormList 快速排序的表单对象
     * @return int
     * Author summer
     * Version 2.0.0-RELEASE
     * DateTime 2019-10-15 15:53
     */
    @ApiOperation(value = "排序", notes = "对角色进行排序的接口")
    @PutMapping("/easy_sort")
    public int easySort(@NotNull(message = "排序数据不能为空") @Valid
                        @RequestBody List<BaseSortForm> baseSortFormList) {
        try {
            // 执行排序并返回结果
            return userService.sort(baseSortFormList, User.class);
        } catch (IllegalAccessException | InstantiationException e) {
            e.printStackTrace();
            throw new CommonException("角色排序出错");
        }
    }

    /**
     * 根据传入的条件, 查询符合条件的用户对象的分页数据
     *
     * @param keyWord 查询的关键字
     * @param pageable 分页信息
     * @return io.easyspring.framework.base.pagehelper.PageInfo<io.easyspring.demo.basis.rbac.model.User>
     * Author summer
     * Version 2.0.0-RELEASE
     * DateTime 2019-10-15 15:15:03
     */
    @ApiOperation(value = "分页查询", notes = "根据传入的条件, 查询符合条件的用户对象的分页数据")
    @JsonView(CommonJsonView.SimpleView.class)
    @GetMapping("/page")
    public PageInfo<User> findPage(@RequestParam(required = false) String keyWord, Pageable pageable) {

        // 执行查询并返回结果
        Page<User> pageBuilder = userService.findPageBuilder(keyWord, pageable);

        // 返回查询结果
        return userService.buildPageInfo(pageBuilder);
    }
}

